package com.huatai.exchange.impl;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.common.utils.StringUtil;
import com.huatai.exchange.AbstractService;
import com.huatai.exchange.dal.model.ExcFileLoadInfo;
import com.huatai.exchange.service.FileLoadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FileLoadServiceImpl extends AbstractService implements FileLoadService {

    private static final Logger logger = LoggerFactory.getLogger(FileLoadService.class);

    @Value("${tmp_file_path}")
    private String tmpFilePath;

    @Override
    public void checkFileAvailableMark(ExcFileLoadInfo loadInfo, List<String> smbFileNames, Map<String, Object> resvMap) throws BusinessException {
        if (ListUtils.isEmpty(smbFileNames)) {
            throw new BusinessException("标志文件未到");
        }
        String markName = formatStr(loadInfo.getMarkName(), resvMap);
        String[] markNames = markName.split(",");
        for (String name : markNames) {
            boolean b = false;
            for (String smbFilePath : smbFileNames) {
                logger.info("smbFilePath:" + smbFilePath);
                if (smbFilePath.contains(name)) {
                    b = true;
                    break;
                }
                if (name.contains("*") && StringUtil.wildcardMatch(smbFilePath, name)) {
                    b = true;
                    break;
                }
            }
            if (!b) {
                throw new BusinessException("标志文件[" + name + "]未到");
            }
        }
    }

    @Override
    public void downloadFile(ExcFileLoadInfo loadInfo, Map<String, Object> resvMap) throws BusinessException {

    }

//    @Override
//    public void downloadAndZipFiles(String downloadFileName,String srcName, String smbDirUrl, List<String> smbFileNames, Map<String, Object> resvMap) throws BusinessException {
//        File tmpDir = null;
//        try {
//            String tmpDirPath = tmpFilePath + UUID.randomUUID().toString() + "/";
//            tmpDir = new File(tmpDirPath);
//            tmpDir.mkdirs();
//            List<String> filePathList = Lists.newArrayList();
//            NtlmPasswordAuthentication auth = SmbFileUtils.createAuthHdisk();
//            for (String smbFileName : smbFileNames) {
//                // 文件名匹配
//                if (StringUtil.wildcardMatch(smbFileName, srcName)) {
//                    logger.info("文件：" + smbFileName);
//                    String path = tmpDirPath + smbFileName;
//                    File destFile = new File(path);
//                    String smbFileUrl = smbDirUrl+ smbFileName ;
//                    SmbFileUtils.copyLocalFileFromRemote(destFile, smbFileUrl, auth);
//                    filePathList.add(path);
//                }
//            }
//            ZipUtil.zip(downloadFileName, filePathList);
//            logger.info("压缩完成");
//        } catch (Exception ex) {
//            BusinessException.throwBusinessException(ex);
//        } finally {
//            tmpDir.deleteOnExit();
//        }
//    }
}
